﻿using APP.QUANLY.Common;
using MetroUI.Forms;
using RMAS.DAL;
using Sys;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RMAS
{
    public partial class frmDM_Meterial : Form
    {
        EnumGUI.ThaoTac thaotac;
        DM_METERIAL_Provider prov = null;
        DM_METERIAL Currobj = null;
        private SqlConnection con;

        public frmDM_Meterial(DM_METERIAL _obj, EnumGUI.ThaoTac _thaotac)
        {
            InitializeComponent();
            this.thaotac = _thaotac;
            this.Currobj = _obj;
        }
        private void frmDM_Meterial_Load(object sender, EventArgs e)
        {
            LoadAll();
        }
        private void LoadAll()
        {
            con = (SqlConnection)StaticVar.ServerConnection;
            prov = new DM_METERIAL_Provider();
            LoadData();
            ControlUI(thaotac);
        }
        public void LoadData()
        {
            LoadToCmb_GroupMeterial();
            LoadToCmb_Unit();
        }
        private void ControlUI(EnumGUI.ThaoTac thaotac)
        {
            if (thaotac == EnumGUI.ThaoTac.them || thaotac == EnumGUI.ThaoTac.sua)
            {
                SetStateButton(thaotac);
                SetTextReadOnly(false);
                loadToUI(Currobj);
                UIHelper.LoadFocus(txtName);
            }
            if (thaotac == EnumGUI.ThaoTac.xem)
            {
                SetStateButton(thaotac);
                SetTextReadOnly(true);
                loadToUI(Currobj);
            }
        }
        private void SetStateButton(EnumGUI.ThaoTac thaotac)
        {
            switch (thaotac)
            {
                case EnumGUI.ThaoTac.them:
                    btnLuuThem.Visible = true;
                    btnLuuThoat.Visible = true;
                    btnMacDinh.Visible = true;
                    break;
                case EnumGUI.ThaoTac.sua:
                    btnLuuThem.Visible = false;
                    btnLuuThoat.Visible = true;
                    btnMacDinh.Visible = true;
                    break;
                case EnumGUI.ThaoTac.xem:
                    btnLuuThem.Visible = false;
                    btnLuuThoat.Visible = false;
                    btnMacDinh.Visible = false;
                    break;
            }

        }
        private void SetTextReadOnly(bool IsReadOnly)
        {
            txtName.ReadOnly = IsReadOnly;
            cmbGroupMeterial.Enabled = !IsReadOnly;
            cmbUnitMeterial.Enabled = !IsReadOnly;
            numGia.Enabled = !IsReadOnly;
            numMax.Enabled = !IsReadOnly;
            numMin.Enabled = !IsReadOnly;
            chbIsUse.Enabled = !IsReadOnly;
        }

        private void LoadToCmb_GroupMeterial()
        {
            try
            {
                DM_METERIALGROUPList list = StaticVar.DanhSachNhomNguyenVatLieu;
                cmbGroupMeterial.DataSource = null;

                if (list.Count==0)
                {
                    if (MessageBox.Show("Chưa có danh sách nhóm nvl!\nVui lòng nhập danh sách nvl trước!\nBạn có muốn nhập thêm nhóm nvl không?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        frmDM_GroupMeterial Grmeterial = new frmDM_GroupMeterial(null, EnumGUI.ThaoTac.them);
                        if (Grmeterial.ShowDialog() == DialogResult.OK) list = StaticVar.DanhSachNhomNguyenVatLieu;
                    }
                    else
                    {
                        this.DialogResult = DialogResult.Cancel;
                        base.Close();
                    }
                }
                DataTable data = new DataTable();
                data.Columns.Add("Mã", typeof(string));
                data.Columns.Add("nhóm nvl", typeof(string));

                foreach (DM_METERIALGROUP obj in list)
                {
                    data.Rows.Add(obj.ID, obj.Name);
                }

                cmbGroupMeterial.DataSource = data;
                cmbGroupMeterial.DisplayMember = "nhóm nvl";
                cmbGroupMeterial.ValueMember = "Mã";
                cmbGroupMeterial.ColumnWidths = "0,200";
                cmbGroupMeterial.SelectedIndex = -1;
                cmbGroupMeterial.Text = "";
            }
            catch { }
        }
        private void LoadToCmb_Unit()
        {
            try
            {
                DM_METERIALUNITList list = StaticVar.DanhSachDonViNVL;
                cmbUnitMeterial.DataSource = null;

                if (list.Count == 0)
                {
                    if (MessageBox.Show("Chưa có danh sách đơn vị nvl!\nVui lòng nhập danh sách đơn vị nvl trước!\nBạn có muốn nhập đơn đơn vị nvl không?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        frmDM_MeterialUnit unit = new frmDM_MeterialUnit(null, EnumGUI.ThaoTac.them);
                        if (unit.ShowDialog() == DialogResult.OK) list = StaticVar.DanhSachDonViNVL;
                        if (list.Count == 0) return;    
                    }
                    else
                    {
                        this.DialogResult = DialogResult.Cancel;
                        base.Close();
                    }
                }
                DataTable data = new DataTable();
                data.Columns.Add("Mã", typeof(string));
                data.Columns.Add("đơn vị nvl", typeof(string));
                foreach (DM_METERIALUNIT obj in list)
                {
                    data.Rows.Add(obj.ID, obj.Name);
                }

                cmbUnitMeterial.DataSource = data;
                cmbUnitMeterial.DisplayMember = "đơn vị nvl";
                cmbUnitMeterial.ValueMember = "Mã";
                cmbUnitMeterial.ColumnWidths = "0,200";
                cmbUnitMeterial.SelectedIndex = -1;
                cmbUnitMeterial.Text = "";
            }
            catch { }
        }
        private bool loadToUI(DM_METERIAL obj)
        {
            bool bKQ = false;
            try
            {
                if (obj == null)
                {
                    obj = new DM_METERIAL();
                    DM_METERIALList list = prov.GetAll();
                    if (list == null) obj.ID = "NVL.1";
                    else obj.ID = "NVL." + (list.Count + 1).ToString();
                    obj.IDPOS = StaticVar.CPUID;
                    obj.IsUsed = "SĐ";
                }

                txtID.Text = obj.ID;
                labPosID.Text = obj.IDPOS;
                labUintID.Text = obj.UnitID;
                labGrID.Text = obj.GroupID;

                txtName.Text = obj.Name;
                numGia.Value = (Decimal)obj.Price;
                numMax.Value = (Decimal)obj.MaxNumStore;
                numMin.Value = (Decimal)obj.MinNumStore;

                if (obj.IsUsed == "SĐ") chbIsUse.Checked = true;
                else chbIsUse.Checked = false;
                if (StaticVar.DanhSachNhomNguyenVatLieu != null && StaticVar.DanhSachNhomNguyenVatLieu.Count > 0)
                {
                    DM_METERIALGROUP objGr = StaticVar.DanhSachNhomNguyenVatLieu.Find(obj.GroupID);
                    if (objGr != null) cmbGroupMeterial.Text = objGr.Name;
                }

                if (StaticVar.DanhSachDonViNVL != null && StaticVar.DanhSachDonViNVL.Count > 0)
                {
                    DM_METERIALUNIT objunit = StaticVar.DanhSachDonViNVL.Find(obj.GroupID);
                    if (objunit != null) cmbUnitMeterial.Text = objunit.Name;
                }
                bKQ = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi :" + ex.Message, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            return bKQ;
        }
        private DM_METERIAL loadToValues()
        {
            DM_METERIAL obj = new DM_METERIAL();
            try
            {
                if (labPosID.Text != "0" && txtName.Text != "")
                {
                    obj.ID = txtID.Text;
                    obj.IDPOS = labPosID.Text;
                    obj.Name = txtName.Text;
                    if (chbIsUse.Checked) obj.IsUsed = "SĐ";
                    else obj.IsUsed = "--";
                    obj.Price = (double)numGia.Value;
                    obj.MaxNumStore = (double)numMax.Value;
                    obj.MinNumStore = (double)numMin.Value;
                    obj.GroupID = labGrID.Text;
                    obj.UnitID = labUintID.Text;
                }
                else
                {
                    MessageBox.Show("Vui lòng nhập đầy đủ thông tin!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    obj = null;
                }
            }
            catch
            {
                obj = null;
            }
            return obj;
        }
        private bool cmd_XuLy(EnumGUI.ThaoTac thaotac)
        {
            bool KQ = false;
            if (thaotac == EnumGUI.ThaoTac.macdinh) KQ = loadToUI(Currobj);
            else
            {
                DM_METERIAL obj = loadToValues();
                if (obj != null)
                {
                    switch (thaotac)
                    {
                        case EnumGUI.ThaoTac.them:
                            KQ = prov.Cmd_ThemDM_METERIAL(obj);
                            break;
                        case EnumGUI.ThaoTac.sua:
                            KQ = prov.Cmd_CapNhatDM_METERIAL(obj);
                            break;
                    }
                    if (prov.ErrMsg != null)
                        MessageBox.Show(prov.ErrMsg.Replace("DM_METERIAL", "nguyên vật liệu"), "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            return KQ;
        }

        private void btn_Click(object sender, EventArgs e)
        {
            bool KQ = false;
            Button btn = (Button)sender;
            string _thaotac = btn.Tag.ToString();
            switch (_thaotac)
            { 
                case "luuthoat":
                    KQ = cmd_XuLy(thaotac);
                    if (KQ)
                    {
                        this.DialogResult = DialogResult.OK;
                        base.Close();
                    }
                    break;
                case "luuthem":
                    KQ = cmd_XuLy(thaotac);
                    if (KQ)
                    {
                        ControlUI(EnumGUI.ThaoTac.them);
                    }
                    break;
                case "macdinh":
                    KQ = cmd_XuLy(EnumGUI.ThaoTac.macdinh);
                    break;
                case "themnhom":
                    frmDM_GroupMeterial faddGr = new frmDM_GroupMeterial(null, EnumGUI.ThaoTac.them);
                    if (faddGr.ShowDialog() == DialogResult.OK)
                    {
                        LoadToCmb_GroupMeterial();
                    }
                    break;
                case "themdonvi":
                    frmDM_MeterialUnit faddUnit = new frmDM_MeterialUnit(null, EnumGUI.ThaoTac.them);
                    if (faddUnit.ShowDialog() == DialogResult.OK)
                    {
                        LoadToCmb_Unit();
                    }
                    break;
                case "thoat":
                    this.DialogResult = DialogResult.OK;
                    base.Close();
                    break;
            }
            StaticVar.DanhSachNguyenVatLieu = prov.GetAll();
            UIHelper.LoadFocus(txtName);
        }
        private void txt_Enter(object sender, EventArgs e)
        {
            UIHelper.txt_Enter(sender, e);
        }
        private void txt_Leave(object sender, EventArgs e)
        {
            UIHelper.txt_Leave(sender, e);
        }
        private void txt_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                TextBox txt = sender as TextBox;
                if (txt.Name == txtName.Name)
                    UIHelper.txt_KeyPress(sender, cmbGroupMeterial, e);
                if (txt.Name == cmbUnitMeterial.Name)
                    UIHelper.txt_KeyPress(sender, numGia, e);
                if (txt.Name == numGia.Name)
                    UIHelper.txt_KeyPress(sender, numMin, e);
                if (txt.Name == numMin.Name)
                    UIHelper.txt_KeyPress(sender, numMax, e);
                if (txt.Name == numMax.Name)
                    UIHelper.txt_KeyPress(sender, btnLuuThoat, e);
            }
            catch { }
        }

        private void cmbGroupMeterial_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbGroupMeterial.SelectedIndex >= 0)
            {
                DataRowView drvData = (DataRowView)cmbGroupMeterial.SelectedItem;
                string Ma = drvData.Row.ItemArray[0].ToString();
                string Ten = drvData.Row.ItemArray[1].ToString();
                labGrID.Text = Ma;
                cmbGroupMeterial.Text = Ten;
            }
        }
        private void cmbGroupMeterial_OpenSearchForm(object sender, EventArgs e)
        {
            frmSearchInfo frmSearch = new frmSearchInfo((MultiColumnComboBox)sender);
            frmSearch.ShowDialog();
            if (cmbGroupMeterial.SelectedIndex >= 0)
            {
                DataRowView drvData = (DataRowView)cmbGroupMeterial.SelectedItem;
                string Ma = drvData.Row.ItemArray[0].ToString();
                string Ten = drvData.Row.ItemArray[1].ToString();
                labGrID.Text = Ma;
                cmbGroupMeterial.Text = Ten;
            }
        }
        private void cmbUnitMeterial_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbUnitMeterial.SelectedIndex >= 0)
            {
                DataRowView drvData = (DataRowView)cmbUnitMeterial.SelectedItem;
                string Ma = drvData.Row.ItemArray[0].ToString();
                string Ten = drvData.Row.ItemArray[1].ToString();
                labUintID.Text = Ma;
                cmbUnitMeterial.Text = Ten;
            }
        }
        private void cmbUnitMeterial_OpenSearchForm(object sender, EventArgs e)
        {
            frmSearchInfo frmSearch = new frmSearchInfo((MultiColumnComboBox)sender);
            frmSearch.ShowDialog();
            if (cmbUnitMeterial.SelectedIndex >= 0)
            {
                DataRowView drvData = (DataRowView)cmbUnitMeterial.SelectedItem;
                string Ma = drvData.Row.ItemArray[0].ToString();
                string Ten = drvData.Row.ItemArray[1].ToString();
                labUintID.Text = Ma;
                cmbUnitMeterial.Text = Ten;
            }
        }
    }
}
